package org.hpin.asset.web;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.hpin.asset.entity.Assetinfo;
import org.hpin.asset.entity.Borrowinfo;
import org.hpin.asset.entity.Distributed;
import org.hpin.asset.entity.UpgradeAsset;
import org.hpin.asset.service.AssetinfoService;
import org.hpin.asset.service.BorrowinfoService;
import org.hpin.asset.service.DistributedService;
import org.hpin.asset.service.UpgradeAssetService;
import org.hpin.common.core.SpringTool;
import org.hpin.common.core.web.BaseAction;
import org.hpin.common.util.HttpTool;
import org.hpin.common.util.StrUtils;
import org.hpin.common.widget.pagination.Page;

/**
 * 
 * @author Charlolo
 *
 */
@Namespace("/asset")
@Action("assetAction")
@Results({
	@Result(name="listAsset" ,location = "/WEB-INF/asset/listAsset.jsp"),
	@Result(name="addAsset" ,location = "/WEB-INF/asset/addAsset.jsp"),
	@Result(name="distributedAssetAll", location = "/WEB-INF/asset/distributedAssetAll.jsp"),
	@Result(name="distributedAsset", location = "/WEB-INF/asset/distributedAsset.jsp"),
	@Result(name="browAssetAll", location = "/WEB-INF/asset/browAssetAll.jsp"),
	@Result(name="upgradeAsset", location = "/WEB-INF/asset/upgradeAsset.jsp"),
	@Result(name="upgradeAssetAll", location = "/WEB-INF/asset/upgradeAssetAll.jsp"),
	@Result(name="browAsset", location = "/WEB-INF/asset/browAsset.jsp"),
	@Result(name="modifyAssetAll", location = "/WEB-INF/asset/modifyAssetAll.jsp"),
	@Result(name="modifyAsset", location = "/WEB-INF/asset/modifyAsset.jsp"),
	@Result(name="browDistributed", location = "/WEB-INF/asset/browDistributed.jsp"),
	@Result(name="browUpgrade", location = "/WEB-INF/asset/browUpgrade.jsp"),
	})
public class AssetAction extends BaseAction{
	AssetinfoService service=(AssetinfoService) SpringTool.getBean(AssetinfoService.class);
	DistributedService distributedService=(DistributedService) SpringTool.getBean(DistributedService.class);
	UpgradeAssetService upgradeAssetService=(UpgradeAssetService) SpringTool.getBean(UpgradeAssetService.class);
	BorrowinfoService borrowinfoService=(BorrowinfoService) SpringTool.getBean(BorrowinfoService.class);
	private Assetinfo assetInfo;
	private UpgradeAsset upgradeAsset;
	private Borrowinfo borrowinfo;
	public Assetinfo getAssetInfo() {
		return assetInfo;
	}
	public void setAssetInfo(Assetinfo assetInfo) {
		this.assetInfo = assetInfo;
	}
	/**
	 * 查询所有资产
	 * @return
	 * @throws ParseException
	 */
	public String listAsset() throws ParseException{
		page = new Page(HttpTool.getPageNum() , HttpTool.getPageSize());
		Map paramsMap = buildSearch();
		String orderField  = HttpTool.getParameter("orderField");
		String orderDirection = HttpTool.getParameter("orderDirection");
		if( paramsMap.containsKey("orderDirection")){
			paramsMap.remove("orderDirection");
		}
		if( paramsMap.containsKey("orderField")){
			paramsMap.remove("orderField");
		}
		if (StrUtils.isNotNullOrBlank(orderField)&&StrUtils.isNotNullOrBlank(orderDirection)) {
			paramsMap.put(orderField,orderDirection);
		}
		HttpTool.setAttribute("orderField", orderField);
		HttpTool.setAttribute("orderDirection", orderDirection);
		findByPage(page, paramsMap);
		return "listAsset";
	}
	public Page findByPage(Page page , Map paramsMap){
		paramsMap.put("filter_and_isDeleted_EQ_I",0);
		service.findByPage(page, paramsMap);
		return page ;
	}
	/**
	 * 删除资产
	 * @return
	 */
	public String deleteAsset(){
		JSONObject json = new JSONObject();
		try {
			String[] id = ids.replaceAll(" ", "").split(",");
			Assetinfo assetinfo=null;
			boolean flag=true;
			for(int i=0;i< id.length;i++){
				assetinfo=(Assetinfo) service.findById(id[i]);
				if(assetinfo!=null && assetinfo.getState().equals("1010101")){
					service.deleteById(assetinfo.getId());
					json.accumulate("statusCode", 200);
					json.accumulate("message", "操作成功已删除"+assetinfo.getName());
					json.accumulate("navTabId", super.navTabId);
					json.accumulate("callbackType", "");
					json.accumulate("forwardUrl", "");
					json.accumulate("confirmMsg", "");
				}else{
					flag=false;
				}
			}
			if(!flag){
				json.accumulate("statusCode",300);
				json.accumulate("message", "只有入库的资产才能删除！");
			}
		} catch (Exception e) {
			json.accumulate("statusCode", 300);
			json.accumulate("message", "操作失败");
			e.printStackTrace();
		}
		renderJson(json);
		return null;
		
	}
	/**
	 * 资产详情
	 * @return
	 * @throws ParseException
	 */
	public String browAssetAll() throws ParseException{
		String flag = HttpTool.getParameter("flag");
		HttpTool.setAttribute("id", id);
		HttpTool.setAttribute("flag", flag);
		return "browAssetAll";
	}
	/**
	 * 查看资产详情
	 * @return
	 */
	public String browAsset(){
		String flag = HttpTool.getParameter("flag");
		assetInfo=(Assetinfo) service.findById(id);
		List<UpgradeAsset> upList=upgradeAssetService.findUpgradeAssetByAttribute(UpgradeAsset.class, "assetId", id);
		HttpTool.setAttribute("upList", upList);
		if(StrUtils.isNotNullOrBlank(assetInfo.getState())){
			if(assetInfo.getState().equals("1010105")){
				Borrowinfo borrow=(Borrowinfo) borrowinfoService.findClassByAttribute(Borrowinfo.class, "assetno", id).get(0);
				HttpTool.setAttribute("borrow", borrow);
			}
		}
		/*if(StrUtils.isNotNullOrBlank(assetInfo.getIsIn())){
			if(assetInfo.getIsIn().equals("1010301")){*/
				System.out.println(id+assetInfo.getIsIn());
				if(distributedService.findDistributedByAttribute(Distributed.class,"assetId", id).size()>0){
					Distributed dis=(Distributed) distributedService.findDistributedByAttribute(Distributed.class,"assetId", id).get(0);
					System.out.println(dis.getCompany());
					HttpTool.setAttribute("dis", dis);
				}
		/*	}
		}*/
		HttpTool.setAttribute("flag", flag);
		return "browAsset";
	}
	/**
	 * 跳转升级
	 * @return
	 */
	public String upgradeAssetAll(){
		String flag = HttpTool.getParameter("flag");
		HttpTool.setAttribute("id", id);
		HttpTool.setAttribute("flag", flag);
		return "upgradeAssetAll";
	}
	/**
	 * 升级
	 * @return
	 */
	public String upgradeAsset(){
		if(StrUtils.isNotNullOrBlank(id)){
			assetInfo = (Assetinfo)service.findById(id);
		}else{
			assetInfo = new Assetinfo();
		}
		return "upgradeAsset";
	}
	/**
	 * 修改资产信息前查询
	 */
	public String modifyAssetAll(){
		HttpTool.setAttribute("id", id);
		return "modifyAssetAll";
	}
	
	/** 修改资产页面*/
	public String modifyAsset(){
		assetInfo = (Assetinfo)service.findById(id);
		return "modifyAsset";
	}
	public String addAsset(){
		if(StrUtils.isNotNullOrBlank(id)){
			assetInfo = (Assetinfo)service.findById(id);
			
		}else{
			assetInfo = new Assetinfo();
		}
		return "addAsset";
	}
	/* 添加资产基本信息*/
	/*public String saveAsset(){
		JSONObject json = new JSONObject();
		try {
			String currentTabId = HttpTool.getParameter("currentTabId");
			if(StrUtils.isNotNullOrBlank(HttpTool.getParameter("aId"))){
				upgradeAsset.setAssetId(HttpTool.getParameter("aId"));
				upgradeAsset.setUpgradeTime(new Date());
				upgradeAssetService.save(upgradeAsset);
				json.accumulate("statusCode", 200);
				json.accumulate("message", "操作成功");
				json.accumulate("navTabId", super.navTabId);
				json.accumulate("currentTabId", currentTabId);
				json.accumulate("callbackType", "closeCurrent");
			}else{
			if(StrUtils.isNotNullOrBlank(assetInfo.getId())){
				if(null!=assetInfo){
					if(null!=distributed){
						Assetinfo asset=(Assetinfo) service.findById(assetInfo.getId());
						asset.setStorageplace(HttpTool.getParameter("storageplace"));
						asset.setDepartment(HttpTool.getParameter("department"));
						asset.setPrincipal(HttpTool.getParameter("principal"));
						asset.setIsIn(HttpTool.getParameter("isIn"));
						asset.setUserId(HttpTool.getParameter("userId"));
						asset.setState("1010102");
						service.update(asset);
						distributed.setAssetId(assetInfo.getId());
						distributed.setState(0);
						distributedService.updateDistributed(distributed,asset.getId());
						json.accumulate("statusCode", 200);
						json.accumulate("message", "操作成功");
						json.accumulate("navTabId", super.navTabId);
						json.accumulate("currentTabId", currentTabId);
						json.accumulate("callbackType", "closeCurrent");
					}else{
						System.out.println(assetInfo.getId());
						service.update(assetInfo);
						json.accumulate("statusCode", 200);
						json.accumulate("message", "操作成功");
						json.accumulate("navTabId", super.navTabId);
						json.accumulate("currentTabId", currentTabId);
						json.accumulate("callbackType", "closeCurrent");
					}
				}
			}else{
				assetInfo.setId(null);
				assetInfo.setIsDeleted(0);
				assetInfo.setStartusetime(new Date());
				assetInfo.setState("1010101");
				service.save(assetInfo);
				System.out.println("assetinfoId="+assetInfo.getId());
				json.accumulate("statusCode", 200);
				json.accumulate("message", "操作成功");
				json.accumulate("navTabId", super.navTabId);
				json.accumulate("currentTabId", currentTabId);
				json.accumulate("callbackType", "closeCurrent");
			}
			}
		} catch (Exception e) {
			json.accumulate("statusCode", 300);
			json.accumulate("message", "操作失败");
			e.printStackTrace();
		}
		//json.accumulate("callbackType", asset.getId());
		renderJson(json);
		return null;
	}*/
	/** 升级资产*/
	public String upgrade(){
		JSONObject json = new JSONObject();
		try {
			String currentTabId = HttpTool.getParameter("currentTabId");
			if(StrUtils.isNotNullOrBlank(HttpTool.getParameter("aId"))){
				upgradeAsset.setAssetId(HttpTool.getParameter("aId"));
				Assetinfo asset=(Assetinfo) service.findById(HttpTool.getParameter("aId"));
				HttpTool.getRequest().setAttribute("assetInfo", asset);
				upgradeAsset.setUpgradeTime(new Date());
				upgradeAssetService.save(upgradeAsset);
				HttpTool.getRequest().setAttribute("upgrAdeAsset", upgradeAsset);
				json.accumulate("statusCode", 200);
				json.accumulate("message", "操作成功");
				json.accumulate("navTabId", super.navTabId);
				json.accumulate("currentTabId", currentTabId);
		        json.accumulate("callbackType", "closeCurrent");
				}
		}catch (Exception e) {
			json.accumulate("statusCode", 300);
			json.accumulate("message", "操作失败");
			e.printStackTrace();
		}
		//json.accumulate("callbackType", asset.getId());
		renderJson(json);
		return null;
	}
	/**
	*派发资产
	*/
	public String distributed(){
		JSONObject json = new JSONObject();
		try {
			String currentTabId = HttpTool.getParameter("currentTabId");
				Assetinfo asset=(Assetinfo) service.findById(assetInfo.getId());
				HttpTool.getRequest().setAttribute("assetInfo", asset);
				asset.setStorageplace(HttpTool.getParameter("storageplace"));
				asset.setDepartment(HttpTool.getParameter("department"));
				asset.setPrincipal(HttpTool.getParameter("principal"));
				asset.setIsIn(HttpTool.getParameter("isIn"));
				asset.setUserId(HttpTool.getParameter("userId"));
				asset.setState("1010102");
				service.update(asset);
				distributed.setAssetId(asset.getId());
				distributed.setState(0);
				distributed.setIsIn(asset.getIsIn());
				distributed.setPlace(asset.getStorageplace());
				distributed.setUserId(asset.getUserId());
				distributed.setUserName(asset.getPrincipal());
				distributed.setDeptId(asset.getDepartment());
				distributed.setPlace(asset.getStorageplace());
				//distributedService.updateDistributed(distributed,asset.getId());
				distributedService.save(distributed);
				HttpTool.getRequest().setAttribute("distriButed",distributed);
				json.accumulate("statusCode", 200);
				json.accumulate("message", "操作成功");
				json.accumulate("navTabId", super.navTabId);
				json.accumulate("currentTabId", currentTabId);
				json.accumulate("callbackType", "closeCurrent");
		} catch (Exception e) {
			json.accumulate("statusCode", 300);
			json.accumulate("message", "操作失败");
			e.printStackTrace();
		}
		//json.accumulate("callbackType", asset.getId());
		renderJson(json);
		return null;
	}
	/**
	*修改资产
	*/
	public String update(){
		JSONObject json = new JSONObject();
		try {
			String currentTabId = HttpTool.getParameter("currentTabId");
			service.update(assetInfo);
			json.accumulate("statusCode", 200);
			json.accumulate("message", "操作成功");
			json.accumulate("navTabId", super.navTabId);
			json.accumulate("currentTabId", currentTabId);
			json.accumulate("callbackType", "closeCurrent");
			} catch (Exception e) {
			json.accumulate("statusCode", 300);
			json.accumulate("message", "操作失败");
			e.printStackTrace();
		}
		//json.accumulate("callbackType", asset.getId());
		renderJson(json);
		return null;
	}
	/**
	*添加资产
	*/
	public String saveAsset(){
		JSONObject json = new JSONObject();
		try {
			String currentTabId = HttpTool.getParameter("currentTabId");
			assetInfo.setId(null);
			assetInfo.setIsDeleted(0);
			assetInfo.setStartusetime(new Date());
			assetInfo.setState("1010101");
			service.save(assetInfo);
			HttpTool.getRequest().setAttribute("assetInfo", assetInfo);
			System.out.println("assetinfoId="+assetInfo.getId());
			json.accumulate("statusCode", 200);
			json.accumulate("message", "操作成功");
			json.accumulate("navTabId", super.navTabId);
			json.accumulate("currentTabId", currentTabId);
			json.accumulate("callbackType", "closeCurrent");
	} catch (Exception e) {
			json.accumulate("statusCode", 300);
			json.accumulate("message", "操作失败");
			e.printStackTrace();
		}
		//json.accumulate("callbackType", asset.getId());
		renderJson(json);
		return null;
	}
	/**
	 * 跳转派发
	 * @return
	 */
	public String distributedAssetAll(){
		HttpTool.setAttribute("AssetId", id);
		return "distributedAssetAll";
	}
	/**
	 * 派发
	 * @return
	 */
	public String distributedAsset(){
		assetInfo = (Assetinfo)service.findById(id);
		return "distributedAsset";
	}
	/**
	 * 查看升级操作详情
	 */
	public String browUpgrade(){
		assetInfo = (Assetinfo)service.findById(id);
		upgradeAsset = (UpgradeAsset)upgradeAssetService.findById(id);
		return "browUpgrade";
	}
	/**
	 * 查看派发操作详情
	 * @return
	 */
	public String browDistributed(){
		//assetInfo = (Assetinfo)service.findById(id);
		distributed = (Distributed) distributedService.findById(id);
		assetInfo=(Assetinfo) service.findById(distributed.getAssetId());
		return "browDistributed";
	}
	/**
	 * 
	 * @return
	 */
	public String returnAsset(){
		JSONObject json = new JSONObject();
		try {
			String currentTabId = HttpTool.getParameter("currentTabId");
			String[] id = ids.replaceAll(" ", "").split(",");
			boolean flag=true;
			for(int i=0;i< id.length;i++){
				assetInfo=(Assetinfo) service.findById(id[i]);
				if(assetInfo.getState().equals("1010102") || assetInfo.getState().equals("1010105")
						|| assetInfo.getState().equals("1010106")){
					assetInfo.setUserId(null);
					assetInfo.setDepartment(null);
					assetInfo.setIsIn(null);
					assetInfo.setPrincipal(null);
					assetInfo.setStorageplace(null);
					assetInfo.setState("1010101");
					service.update(assetInfo);
					json.accumulate("statusCode", 200);
					json.accumulate("message", "操作成功");
					json.accumulate("navTabId", super.navTabId);
					json.accumulate("currentTabId", currentTabId);
					//json.accumulate("callbackType", "closeCurrent");
				}else{
					flag=false;
				}
			}
			if(!flag){
				json.accumulate("statusCode", 300);
				json.accumulate("message", "不是使用、借用和入驻状态不能归还！");
			}
		} catch (Exception e) {
			json.accumulate("statusCode", 300);
			json.accumulate("message", "操作失败");
			e.printStackTrace();
		}
		renderJson(json);
		return null;
	}
	public Distributed getDistributed() {
		return distributed;
	}
	public void setDistributed(Distributed distributed) {
		this.distributed = distributed;
	}
	public UpgradeAsset getUpgradeAsset() {
		return upgradeAsset;
	}
	public void setUpgradeAsset(UpgradeAsset upgradeAsset) {
		this.upgradeAsset = upgradeAsset;
	}
	public Borrowinfo getBorrowinfo() {
		return borrowinfo;
	}
	public void setBorrowinfo(Borrowinfo borrowinfo) {
		this.borrowinfo = borrowinfo;
	}
	private Distributed distributed;
}
